package com.mach.platform.sprider.dev;


import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;
import org.joda.time.DateTime;

import java.io.IOException;
import java.util.Map;

public class CoolWaterDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;
    private Map<String, Object> entData;
    private Map<String, Object> tetData;

    public CoolWaterDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }

    @Override
    public void run(){

        long preSec = 0;
        long nowSec = 0;
        while(true){


            try {

               /* long nowMill = System.currentTimeMillis();

                nowSec = nowMill / 1000;

                if(nowSec == preSec)continue;

                Date now = new Date(nowMill);

                int fyear = now.getYear() + 1900;
                int fmonth = now.getMonth() + 1;
                int fday = now.getDate();
                int fhour = now.getHours();
                int fmin = now.getMinutes();
                int fsec = now.getSeconds();*/

                DateTime dateTime = new DateTime();
                int fyear = dateTime.getYear();
                int fmonth = dateTime.getMonthOfYear();
                int fday = dateTime.getDayOfMonth();
                int fhour = dateTime.getHourOfDay();
                int fmin = dateTime.getMinuteOfHour();
                int fsec = dateTime.getSecondOfMinute();

                Integer sum = this.getCoolWaterSumFlow();
                Integer cool = this.getCoolWaterSumCool();
                Float inTemp = this.getCoolWaterImportTemp();
                Float backTemp = this.getCoolWaterBackTemp();


                try {
                    this.sqls.updateAsync("insert ignore into data_detail(dev_id, fyear, fmonth, fday, fhour, fmin, fsec, fdatetime," +
                            "cw_sum,cw_cool,cw_in_temp,cw_back_temp) value" +
                            "('"+this.devName+"',"+fyear+","+fmonth+","+fday+","+fhour+","+fmin+","+fsec+","+"date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s'),"
                            +sum+","+cool+","+inTemp+","+backTemp+")");
                } catch (Exception e) {
                    e.printStackTrace();
                }


                try {
                    this.sqls.updateAsync("replace into data_cur (dev_id,cw_sum,cw_cool,cw_in_temp,cw_back_temp,update_time) value " +
                            "( " +
                            "'"+this.devName+"',"+
                            sum + ","+
                            cool + "," +
                            inTemp + ","+
                            backTemp + "," +
                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s')" +
                            ")");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Boolean warn= false;

                if(this.entData == null || this.tetData == null){
                    this.entData = this.sqls.queryOne("select value,value2 from constants where id=1");
                    this. tetData = this.sqls.queryOne("select value,value2 from constants where id=2");
                }

                Map<String, Object> rangData= null;


                if(null != rangData){
                    Double start = (Double)rangData.get("value");
                    Double end =  (Double)rangData.get("value2");

                    if(start <= inTemp && inTemp <= end){

                    }else{
                        warn = true;
                    }

                }


                if(warn){
                    this.sqls.updateAsync("insert ignore into data_cw_detail (dev_id,code,fdatetime," +
                            "fyear, fmonth, fday, fhour, fmin) value " +
                            "( " +
                            "'"+this.devName+"',"+
                            inTemp + ","+
                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s'),"
                            +fyear+","+fmonth+","+fday+","+fhour+","+fmin+
                            ")");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            preSec = nowSec;


        }


    }

    /*
         冷冻水:供水温度
      */
    public Float getCoolWaterImportTemp() throws IOException {
        Float result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(4, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }

    /*
        冷冻水:回水温度
     */
    public Float getCoolWaterBackTemp() throws IOException {
        Float result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(40, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }
    /*
        冷冻水:累计流量
     */
    public Integer getCoolWaterSumFlow() throws IOException {
        Integer result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(8, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
        //data = ByteUtil.reverse2Byte(data);
        //data = ByteUtil.reverse(data);
        ByteUtil.reveIn2Byte(data, 0);
        ByteUtil.reveIn2Byte(data, 2);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
      冷冻水:累计冷量
   */
    public Integer getCoolWaterSumCool() throws IOException {
        Integer result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(12, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        ByteUtil.reveIn2Byte(data, 0);
        ByteUtil.reveIn2Byte(data, 2);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
     冷冻水:test累计冷量
  */
    public Integer testCoolWaterSumCool() throws IOException {
        Integer result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataTcp(12, 2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        result = ByteUtil.getInt(data);

        return result;
    }

    public static void main(String[] args) throws IOException {
        for(int i = 0; true;++i){
            findAll();
        }
    }

    public static void findAll() throws IOException {
        int busNumStartIx =98,busNumEndIx =99; //总线地址开始与结束参数
        int tcpPortStartIx = 502,tcpPortEndIx = 502; //TCP端口开始与结束参数
        String ip = "192.168.100.1";
        for(int j = tcpPortStartIx; j <= tcpPortEndIx; j+=1){
            for(int i = busNumStartIx; i <= busNumEndIx; ++i){
                //0x015E
                for(int k = 12; k <= 12; ++k){
                    readIdById(ip,j,i,"XXXX",k );
                }
            }
        }
    }
    public static void readIdById(String ip ,Integer port, Integer id,String devName, Integer addr) throws IOException {
        CoolWaterDev dev =  new CoolWaterDev(ip,port,id,devName);
//        byte[] data = dev.readSthTcp(addr, 2, cmdCode);
//        U.printData(data);
    }



}
